<?php

class Exts_Server_XmlRpc extends Zend_XmlRpc_Server
{
	
	
    /**
     * Dispatch method
     * 
     * @param  Zend_Server_Method_Definition $invocable 
     * @param  array $params 
     * @return mixed
     */
    protected function _dispatch(Zend_Server_Method_Definition $invocable, array $params)
    {
    	
        $callback = $invocable->getCallback();
        $type     = $callback->getType();

        if ('function' == $type) {
            $function = $callback->getFunction();
            return call_user_func_array($function, $params);
        }

        $class  = $callback->getClass();
        $method = $callback->getMethod();

        if ('static' == $type) {
            return call_user_func_array(array($class, $method), $params);
        }

        $object = $invocable->getObject();
        if (!is_object($object)) {
            $invokeArgs = $invocable->getInvokeArguments();
            if (!empty($invokeArgs)) {
                $reflection = new ReflectionClass($class);
                $object     = $reflection->newInstanceArgs($invokeArgs);
            } else {
                $object = new $class;
            }
        }
        
        if( strpos( $invocable->getMethodHelp(),'cacheable')){
	        $cacheId = sha1( serialize( array( $object, $method, $params ) ) );
			$res = Zend_Registry::get('xmlrpc_ws_cache')->load( $cacheId );
			if ( $res ) {
				return $res;
			}
			$data = call_user_func_array(array($object, $method), $params);
			Zend_Registry::get('xmlrpc_ws_cache')->save($data, $cacheId);
			return $data;
        }else {
        	return call_user_func_array(array($object, $method), $params);
        }
    }
}